const express = require('express')
const router = express.Router()
const {logI} = require('../log.js')
const {retError, retSuccess} = require('../utils/ret')
const userDb = require('../db/user-db')
const checkType = require('../utils/checkType')
const moment = require('moment')
const crypto = require('crypto')
const checkToken = require('../utils/checkToken')
const checkDeviceNo = require('../utils/checkDeviceNo')
const { v4: uuidv4} = require('uuid');

router
    .post('/', async(req, res) => {
        let {body: {maddr,token,deviceNo}} = req
        logI("A-S", maddr, "delete_user", `${JSON.stringify(req.body)}`, "null")
        try {
            const error = checkType.deleteUser(req.body)
            if(error){
                retError("delete_user",maddr, 10001,  error.details[0].message, res);
            }else{
                let msg = {};
                msg = await checkToken(token,maddr);
                if(msg.code === 0){
                    const deviceInfo={
                        'maddr':maddr,
                        'deviceNo':deviceNo
                    }
                    let device={};
                    device = await checkDeviceNo(deviceInfo);
                    if(device.code === 0){
                        //
                        let result = {};
                        result = await userDb.selectBuildingByEmail(maddr);
                        //console.log(result);
                        let buildingRole0=[];
                        let buildingRole1=[];
                        for(let i = 0;i<result.length;i++){
                            if(result[i].role === 0){
                                buildingRole0.push(result[i].buildingUUID);
                            }else{
                                buildingRole1.push(result[i].buildingUUID);
                            }
                            /*if(result[i].role === 0){
                                await userDb.updateBuildingById(result[i].buildingUUID,result[i].buildingUUID);
                                //await userDb.updateBuildingAll(result[i].buildingUUID,result[i].buildingUUID);
                                //await userDb.updateMeshDel(maddr,result[i].buildingUUID);
                                console.log("111");
                            }else{
                                await userDb.updateBuildingMaddrDel(maddr,result[i].buildingUUID);
                                await userDb.updateMeshDel(maddr,result[i].buildingUUID);
                            }*/
                        }
                        if(buildingRole0.length!==0){
                            await userDb.updateBuildingMaddrByRole0(buildingRole0);
                            //await userDb.updateBuildingById(buildingRole0);
                            await userDb.updateNeshDelByRole0(buildingRole0);
                        }

                        if(buildingRole1.length!==0){
                            await userDb.updateBuildingMaddrByRole1(maddr,buildingRole1);
                            await userDb.updateNeshDelByRole1(maddr,buildingRole1);
                        }
                        await userDb.delUser(maddr);
                        retSuccess("delete_user", maddr,0, "", res);
                    }else{
                        retError("delete_user", maddr,device.code, device.body, res);
                    }
                }else{
                    retError("delete_user", maddr,msg.code, msg.body, res);
                }
            }
        } catch (e) {
            retError("delete_user", maddr, 10001, e, res);
        }
    })

module.exports = router
